<!--  -->
<!-- Alt + Shift + F格式化代码块 -->
<template>
  <div>
    <el-button
       type="danger"  size="mini"  @click="removeByIds()">批量删除
    </el-button>
    <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="api基础路径" width="200"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机"/>

      <el-table-column label="状态" width="80">
        <template slot-scope="scope">{{ scope.row.status === 1 ? '可用' : '不可用' }}</template>
      </el-table-column>
      <el-table-column
         label="操作"  width="280"  align="center">
        <template
           slot-scope="scope">  
          <el-button
             type="danger"  size="mini"  
                     icon="el-icon-delete"  @click="removeDataById(scope.row.id)"> 
          </el-button>
          <el-button v-if="scope.row.status ===1"
                      type="primary"  size="mini"  
                               @click="lockHostSet(scope.row.id,0)">锁定
          </el-button>

          <el-button v-if="scope.row.status ===0"
                      type="primary"  size="mini"  
                                @click="lockHostSet(scope.row.id,1)">解锁
          </el-button>
          <router-link
           :to="'/hospSet/edit/'+scope.row.id">
          <el-button
           type="primary" size="mini" icon="el-icon-edit"></el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"/>

  </div>
</template>


<script>

  import hospset from "@/api/hospset";

  export default {
    components: {},
    data() {
      //这里存放数据
      return {
        list: [],
        current: 1,
        limit: 5,
        searchObj: {},
        total: 0,
        multipleSelection: [] // 批量选择中选择的记录列表
      };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
      this.getList();
    },
    //方法集合
    methods: {
      //锁定与解锁
      lockHostSet(id, status) {
        hospset.lockHospSet(id, status)
          .then(response => {
            this.getList(this.current)
          })
          .catch(
            error => {
              this.$message({
                type: 'error',
                message: error.message
              })
            }
          )
      },
      //多行选择
      handleSelectionChange(selection) {
        this.multipleSelection = selection
      },
      //批量删除
      removeByIds() {
        this.$confirm('此操作将永久删除该医院设置, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var idList = [];
          this.multipleSelection.forEach(item => {
            idList.push(item.id)
          })
          console.log(idList)
          debugger
          hospset.batchRemove(idList)
            .then(response => {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.getList(this.current)
              }
            )
            .catch(error => {
              this.$message({
                type: 'error',
                message: error.message
              })
            })

        })
      },
      //医院设置列表
      getList(page = 1) {
        this.current = page
        //添加当前页参数
        hospset
          .getHospSetList(this.current, this.limit, this.searchObj)
          .then(response => {
            //请求成功response是接口返回数据
            //返回集合赋值list
            console.log(response);
            this.list = response.data.records;
            this.total = response.data.total;
            //总记录数
          })
          .catch(error => {
            //请求失败
            console.log(error);
          });
      },
      //根据ID删除
      removeDataById(id) {
        this.$confirm('此操作将永久删除该医院设置, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          hospset.removeById(id)
            .then(response => {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.getList(this.current)
              }
            )
            .catch(error => {
              this.$message({
                type: 'error',
                message: error.message
              })
            })

        })

      }
    },
  };
</script>
